<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="../css/loginn.css" />
    <script src="../js/jquery-3.6.0.min.js"></script>
    <script src="../js/interface.js"></script>
    <script src="../js/cookie.js"></script>
  </head>

  <body>
    <!-- 引入公共头部 -->
    <div class="header"></div>
    <script type="text/javascript">
      $(".header").load("common/head.html", function () {
        var cookie = document.cookie;
        if (cookie) {
          var username = getCookie("lgc");
          if (username) {
            $(".tipsBar").html(
              `<span>欢迎您,${username} | </span><span onclick="exit()">退出</span>`
            );
          }
        }
      });
    </script>

    <!-- 登录 -->
    <div class="login">
      <div class="log">
        <div class="log-left">
          <h2>还未注册?</h2>
          <p><a href="register.html">注册你的匹克账户></a></p>
          <a href="#"><img src="../images/login-code.jpg" alt="" /></a>
          <p>匹克官方网店微信公众号</p>
          <p>关注不一样的运动商城</p>
        </div>
        <div class="log-right">
          <h2>登录你的匹克会员</h2>
          <p>
            <input type="text" id="username" placeholder="请输入用户名" />
            <span id="user_span"></span>
          </p>
          <p>
            <input type="password" id="pwd" placeholder="请输入密码" />
            <span id="pwd_span"></span>
          </p>
          <p><input type="checkbox" id="isRem" /> 记住用户名?</p>
          <p class="p-password">忘记密码?</p>
          <div id="btn"><a href="javascript:;">登录</a></div>
          <p class="p-vip">线下会员激活</p>
        </div>
      </div>
    </div>
    <footer></footer>
    <script>
      $("footer").load(`common/footer.html`);
    </script>
  </body>

  <script>
    // 用户标识
    var cookie = document.cookie;
    if (cookie) {
      var username = getCookie("lgc");
      if (username) {
        $(".tipsBar").html(
          `<span>欢迎您,${username} | </span><span onclick="exit()">退出</span>`
        );
      }
    }
    function exit() {
      deleteCookie("lgc");
      location.reload();
    }
    //获取url
    function urlParse() {
      var search = location.search;
      var data = {};
      if (search) {
        //从注册页跳转过来 有search数据
        var str = search.substring(1);
        console.log(str);
        var list = str.split("&");
        // console.log(list);
        list.forEach(function (item) {
          var key = item.split("=")[0];
          var val = item.split("=")[1];
          data[key] = val;
        });
      }
      return data;
    }
    //登录
    $("#btn").on("click", async function () {
      var username = $("#username").val();
      var pwd = $("#pwd").val();
      if (username && pwd) {
        var result = await login({ username, pwd });
        var { status, msg } = result;
        if (status) {
          if ($("#idRem").prop("checked")) {
            setCookie("lgc", username, 7);
          } else {
            setCookie("lgc", username);
          }

          var url = urlParse();
          var { returnUrl } = url;
          console.log(returnUrl);
          if (returnUrl) {
            location.href = decodeURIComponent(returnUrl);
          } else {
            location.href = "index.html";
          }
        } else {
          $("#pwd_span").text(msg);
          $("#pwd_span").addClass("err");
        }
      }
    });
  </script>
</html>
